package cn.demoncat.util.seal;

import cn.demoncat.util.io.ImgUtil;
import cn.demoncat.util.seal.entity.CircelSeal;
import cn.demoncat.util.seal.entity.SealCircle;
import cn.demoncat.util.seal.entity.SealFont;
import cn.demoncat.util.seal.entity.SquareSeal;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

/**
 * 印章工具
 * 
 * @author 延晓磊
 *
 * @since 2020年6月9日
 */
public class SealUtil {

	/**
	 * 图形偏移
	 */
	private static final int INIT_BEGIN = 5;
	/**
	 * 中心文字 ★
	 */
	private static final String CENTER_TEXT = "★";
	
	/**
	 * 输出印章
	 * 
	 * @param img	印章图片
	 * @param out	输出流（必须是png格式，已关闭）
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static void out(BufferedImage img, OutputStream out) {
		ImgUtil.out(img, out);
	}
	
	/**
	 * 输出印章
	 * 
	 * @param response
	 * @param img	印章图片
	 * @param name	文件名（如果后缀非空，必须是.png）
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static void out(HttpServletResponse response, BufferedImage img, String name) {
		out(img, ImgUtil.getOutput(response, name, true));
	}
	
	/**
	 * 通用公章：200圆，红色，包含主文字、中心文字"★"、抬头文字
	 * 
	 * @param title	抬头文字（底部，印章类型）
	 * @param text	主文字（顶部，下弧文字）
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static BufferedImage draw(String title, String text) {
		// 公章
		CircelSeal seal = new CircelSeal();
		// 图形尺寸：width&height = 200
		seal.setSize(200);
		// 外环边线：200 = 5*2 + 95 * 2
		seal.setBorderCircle(new SealCircle(4, 95, 95));
		// 顶部名称：margin-top = 4
		seal.setMainFont(new SealFont(text, 22, 22.0, 4));
		// 中心图案：align = center
		seal.setCenterFont(new SealFont(CENTER_TEXT, 60));
		// 底部抬头：margin-top = 5 + 95 + 16/2 + 54(置底，偏离中心图案和主文字圈)
		seal.setTitleFont(new SealFont(title, 16, 8.0, 54));
		return draw(seal);
	}
	
	/**
	 * 通用公章：300*200椭圆，红色，包含主文字、中心文字、抬头文字
	 * 
	 * @param title		抬头文字（底部，印章类型）
	 * @param text		主文字（顶部，下弧文字）
	 * @param remarks	中心文字（中部，印章图案、数据编号、数据描述）
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static BufferedImage draw(String title, String text, String remarks) {
		// 公章
		CircelSeal seal = new CircelSeal();
		// 图形尺寸：width = 300, height = 200
		seal.setWidth(300);
		seal.setHeight(200);
		// 外环边线：300 = 5*2 + 145*2, 200 = 5*2 + 95*2
		seal.setBorderCircle(new SealCircle(3, 145, 95));
		seal.setBorderInnerCircle(new SealCircle(1, 141, 91));
		// 顶部名称：margin-top = 10
		seal.setMainFont(new SealFont(text, 24, 12.0, 10));
		// 中心图案/描述：align = center
		seal.setCenterFont(new SealFont(remarks, StringUtils.length(remarks) == 1 ? 60 : 18));
		// 底部抬头：margin-top = 5 + 145 + 20/2 + 64(置底)
		seal.setTitleFont(new SealFont(title, 20, 10.0, 64));
		return draw(seal);
	}
	
	/**
	 * 通用私章：200方，红色，包含签名
	 * 
	 * @param name	签名（2~4位）
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static BufferedImage draw(String name) {
		// 私章
		SquareSeal seal = new SquareSeal();
		// 边宽
		seal.setBorder(16);
		// 签名
		seal.setNameFont(new SealFont(name, 76));
		return draw(seal);
	}
	
	/**
	 * 绘制公章 - 圆形、椭圆形
	 * 
	 * @param seal	印章
	 * 
	 * @return
	 * @throws Exception
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static BufferedImage draw(CircelSeal seal) {
		// 画布
		BufferedImage bi = new BufferedImage(seal.getWidth(), seal.getHeight(), BufferedImage.TYPE_4BYTE_ABGR);
		// 画笔
		Graphics2D g2d = bi.createGraphics();

		// 抗锯齿设置：文本不抗锯齿，其它图形搞锯齿
		RenderingHints hints = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHints(hints);

		// 透明背景
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 0));
		// 填充画布
		g2d.fillRect(0, 0, seal.getWidth(), seal.getHeight());

		// 非透明背景
		g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, 1));
		// 画笔颜色
		g2d.setPaint(seal.getColor());

		// 画外环外边线圆
		int borderCircleWidth = seal.getBorderCircle().getWidth();
		int borderCircleHeight = seal.getBorderCircle().getHeight();
		drawCircle(g2d, seal.getBorderCircle(), INIT_BEGIN, INIT_BEGIN);

		// 画外环内边线圆
		if (seal.getBorderInnerCircle() != null) {
			int x = INIT_BEGIN + borderCircleWidth - seal.getBorderInnerCircle().getWidth();
			int y = INIT_BEGIN + borderCircleHeight - seal.getBorderInnerCircle().getHeight();
			drawCircle(g2d, seal.getBorderInnerCircle(), x, y);
		}

		// 画内环边线圆
		if (seal.getInnerCircle() != null) {
			int x = INIT_BEGIN + borderCircleWidth - seal.getInnerCircle().getWidth();
			int y = INIT_BEGIN + borderCircleHeight - seal.getInnerCircle().getHeight();
			drawCircle(g2d, seal.getInnerCircle(), x, y);
		}

		// 画弧形主文字：顶部，弧形文字
		if (borderCircleHeight != borderCircleWidth) {
			// 椭圆
			drawArcFont4Oval(g2d, seal.getBorderCircle(), seal.getMainFont(), true);
		} else {
			// 圆
			drawArcFont4Circle(g2d, borderCircleHeight, seal.getMainFont(), true);
		}

		// 画弧形副文字：底部，上弧文字
		if (borderCircleHeight != borderCircleWidth) {
			// 椭圆
			drawArcFont4Oval(g2d, seal.getBorderCircle(), seal.getViceFont(), false);
		} else {
			// 圆
			drawArcFont4Circle(g2d, borderCircleHeight, seal.getViceFont(), false);
		}

		// 画中心文字：中心，印章图案
		drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, seal.getCenterFont());

		// 画抬头文字：底部，印章类型
		drawFont(g2d, (borderCircleWidth + INIT_BEGIN) * 2, (borderCircleHeight + INIT_BEGIN) * 2, seal.getTitleFont());

		// 释放画笔
		g2d.dispose();
		return bi;
	}

	/**
	 * 绘制私章 - 矩形
	 * 
	 * @param seal	印章
	 * 
	 * @return
	 * @throws Exception
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	public static BufferedImage draw(SquareSeal seal) {
		// 画布（便于2-3字拉长，高度预设一半）
		seal.setHeight(seal.getWidth());
		BufferedImage bi = new BufferedImage(seal.getWidth(), seal.getHeight() / 2, BufferedImage.TYPE_4BYTE_ABGR);
		// 画笔
		Graphics2D g2d = bi.createGraphics();
		// 画笔颜色
		g2d.setPaint(seal.getColor());

		// 抗锯齿设置
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		// 画文字
		if (seal.getNameFont().getText().length() == 2) {
			bi = drawFont2(bi, g2d, seal);
		} else if (seal.getNameFont().getText().length() == 3) {
			bi = drawFont3(bi, g2d, seal);
		} else {
			bi = drawFont4(bi, seal);
		}
		// 释放画笔
		g2d.dispose();
		return bi;
	}

	/**
	 * 画圆
	 * 
	 * @param g2d
	 * @param circle
	 * @param x
	 * @param y
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static void drawCircle(Graphics2D g2d, SealCircle circle, int x, int y) {
		if (circle == null) {
			return;
		}
		// 圆线条粗细默认是圆直径的1/35
		int lineSize = circle.getLine() == null ? circle.getHeight() * 2 / (35) : circle.getLine();
		// 画圆
		g2d.setStroke(new BasicStroke(lineSize));
		g2d.drawOval(x, y, circle.getWidth() * 2, circle.getHeight() * 2);
	}

	/**
	 * 绘制椭圆弧形文字
	 * 
	 * @param g2d
	 * @param sealCircle
	 * @param font
	 * @param isTop
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static void drawArcFont4Oval(Graphics2D g2d, SealCircle sealCircle, SealFont font, boolean isTop) {
		if (font == null) {
			return;
		}
		float radiusX = sealCircle.getWidth();
		float radiusY = sealCircle.getHeight();
		float radiusWidth = radiusX + sealCircle.getLine();
		float radiusHeight = radiusY + sealCircle.getLine();

		// 字体长度
		int textLen = font.getText().length();
		// 字体大小，默认根据字体长度动态设定
		int size = font.getSize() == null ? 25 + (10 - textLen) / 2 : font.getSize();
		// 字体样式
		int style = font.getBold() ? Font.BOLD : Font.PLAIN;
		// 构造字体
		Font f = new Font(font.getFamily(), style, size);
		// 总的角跨度
		double totalArcAng = font.getSpace() * textLen;
		// 从边线向中心的移动因子
		float minRat = 0.90f;
		double startAngle = isTop ? -90f - totalArcAng / 2f : 90f - totalArcAng / 2f;
		double step = 0.5;
		int alCount = (int) Math.ceil(totalArcAng / step) + 1;
		double[] angleArr = new double[alCount];
		double[] arcLenArr = new double[alCount];
		int num = 0;
		double accArcLen = 0.0;
		angleArr[num] = startAngle;
		arcLenArr[num] = accArcLen;
		num++;
		double angR = startAngle * Math.PI / 180.0;
		double lastX = radiusX * Math.cos(angR) + radiusWidth;
		double lastY = radiusY * Math.sin(angR) + radiusHeight;
		for (double i = startAngle + step; num < alCount; i += step) {
			angR = i * Math.PI / 180.0;
			double x = radiusX * Math.cos(angR) + radiusWidth, y = radiusY * Math.sin(angR) + radiusHeight;
			accArcLen += Math.sqrt((lastX - x) * (lastX - x) + (lastY - y) * (lastY - y));
			angleArr[num] = i;
			arcLenArr[num] = accArcLen;
			lastX = x;
			lastY = y;
			num++;
		}
		double arcPer = accArcLen / textLen;
		for (int i = 0; i < textLen; i++) {
			double arcL = i * arcPer + arcPer / 2.0;
			double ang = 0.0;
			for (int p = 0; p < arcLenArr.length - 1; p++) {
				if (arcLenArr[p] <= arcL && arcL <= arcLenArr[p + 1]) {
					ang = (arcL >= ((arcLenArr[p] + arcLenArr[p + 1]) / 2.0)) ? angleArr[p + 1] : angleArr[p];
					break;
				}
			}
			angR = (ang * Math.PI / 180f);
			float x = radiusX * (float) Math.cos(angR) + radiusWidth;
			float y = radiusY * (float) Math.sin(angR) + radiusHeight;
			double qxang = Math.atan2(radiusY * Math.cos(angR), -radiusX * Math.sin(angR));
			double fxang = qxang + Math.PI / 2.0;

			int subIndex = isTop ? i : textLen - 1 - i;
			String c = font.getText().substring(subIndex, subIndex + 1);

			// 获取文字高宽
			FontMetrics fm = g2d.getFontMetrics(f);
			int w = fm.stringWidth(c), h = fm.getHeight();
			if (isTop) {
				x += h * minRat * (float) Math.cos(fxang);
				y += h * minRat * (float) Math.sin(fxang);
				x += -w / 2f * (float) Math.cos(qxang);
				y += -w / 2f * (float) Math.sin(qxang);
			} else {
				x += (h * minRat) * (float) Math.cos(fxang);
				y += (h * minRat) * (float) Math.sin(fxang);
				x += w / 2f * (float) Math.cos(qxang);
				y += w / 2f * (float) Math.sin(qxang);
			}

			// 旋转
			AffineTransform affineTransform = new AffineTransform();
			affineTransform.scale(0.8, 1);
			if (isTop)
				affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI - 90)), 0, 0);
			else
				affineTransform.rotate(Math.toRadians((fxang * 180.0 / Math.PI + 180 - 90)), 0, 0);
			Font f2 = f.deriveFont(affineTransform);
			g2d.setFont(f2);
			g2d.drawString(c, (int) x + INIT_BEGIN, (int) y + INIT_BEGIN);
		}
	}

	/**
	 * 绘制圆弧形文字
	 * 
	 * @param g2d
	 * @param circleRadius
	 * @param font
	 * @param isTop
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static void drawArcFont4Circle(Graphics2D g2d, int circleRadius, SealFont font, boolean isTop) {
		if (font == null) {
			return;
		}

		// 字体长度
		int textLen = font.getText().length();
		// 字体大小，默认根据字体长度动态设定
		int size = font.getSize() == null ? (55 - textLen * 2) : font.getSize();
		// 字体样式
		int style = font.getBold() ? Font.BOLD : Font.PLAIN;
		// 构造字体
		Font f = new Font(font.getFamily(), style, size);

		FontRenderContext context = g2d.getFontRenderContext();
		Rectangle2D rectangle = f.getStringBounds(font.getText(), context);

		// 文字之间间距，默认动态调整
		double space;
		if (font.getSpace() != null) {
			space = font.getSpace();
		} else {
			if (textLen == 1) {
				space = 0;
			} else {
				space = rectangle.getWidth() / (textLen - 1) * 0.9;
			}
		}

		// 距离外圈距离
		int margin = font.getMargin() == null ? INIT_BEGIN : font.getMargin();

		// 写字
		double newRadius = circleRadius + rectangle.getY() - margin;
		double radianPerInterval = 2 * Math.asin(space / (2 * newRadius));
		double fix = 0.04;
		if (isTop) {
			fix = 0.18;
		}
		double firstAngle;
		if (!isTop) {
			if (textLen % 2 == 1) {
				firstAngle = Math.PI + Math.PI / 2 - (textLen - 1) * radianPerInterval / 2.0 - fix;
			} else {
				firstAngle = Math.PI + Math.PI / 2 - ((textLen / 2.0 - 0.5) * radianPerInterval) - fix;
			}
		} else {
			if (textLen % 2 == 1) {
				firstAngle = (textLen - 1) * radianPerInterval / 2.0 + Math.PI / 2 + fix;
			} else {
				firstAngle = (textLen / 2.0 - 0.5) * radianPerInterval + Math.PI / 2 + fix;
			}
		}

		for (int i = 0; i < textLen; i++) {
			double theta;
			double thetaX;
			double thetaY;

			if (!isTop) {
				theta = firstAngle + i * radianPerInterval;
			} else {
				theta = firstAngle - i * radianPerInterval;
			}
			thetaX = newRadius * Math.sin(Math.PI / 2 - theta);
			thetaY = newRadius * Math.cos(theta - Math.PI / 2);

			AffineTransform transform;
			if (!isTop) {
				transform = AffineTransform.getRotateInstance(Math.PI + Math.PI / 2 - theta);
			} else {
				transform = AffineTransform.getRotateInstance(Math.PI / 2 - theta + Math.toRadians(8));
			}
			Font f2 = f.deriveFont(transform);
			g2d.setFont(f2);
			g2d.drawString(font.getText().substring(i, i + 1), (float) (circleRadius + thetaX + INIT_BEGIN), (float) (circleRadius - thetaY + INIT_BEGIN));
		}
	}

	/**
	 * 画文字
	 * 
	 * @param g2d
	 * @param circleWidth
	 * @param circleHeight
	 * @param font
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static void drawFont(Graphics2D g2d, int circleWidth, int circleHeight, SealFont font) {
		if (font == null) {
			return;
		}
		// 字体长度
		int textLen = font.getText().length();
		// 字体大小，默认根据字体长度动态设定
		int size = font.getSize() == null ? (55 - textLen * 2) : font.getSize();
		// 字体样式
		int style = font.getBold() ? Font.BOLD : Font.PLAIN;
		// 构造字体
		Font f = new Font(font.getFamily(), style, size);
		g2d.setFont(f);
		// 画文字
		FontRenderContext context = g2d.getFontRenderContext();
		String[] fontTexts = font.getText().split("\n");
		if (fontTexts.length > 1) {
			int y = 0;
			for (String fontText : fontTexts) {
				y += Math.abs(f.getStringBounds(fontText, context).getHeight());
			}
			// 设置上边距
			float margin = INIT_BEGIN + (float) (circleHeight / 2 - y / 2);
			for (String fontText : fontTexts) {
				Rectangle2D rectangle2D = f.getStringBounds(fontText, context);
				g2d.drawString(fontText, (float) (circleWidth / 2 - rectangle2D.getCenterX()), margin);
				margin += Math.abs(rectangle2D.getHeight());
			}
		} else {
			Rectangle2D rectangle2D = f.getStringBounds(font.getText(), context);
			// 设置上边距，默认在中心
			float margin = font.getMargin() == null ? (float) (circleHeight / 2 - rectangle2D.getCenterY()) : (float) (circleHeight / 2 - rectangle2D.getCenterY()) + (float) font.getMargin();
			g2d.drawString(font.getText(), (float) (circleWidth / 2 - rectangle2D.getCenterX()), margin);
		}
	}

	/**
	 * 画2字
	 * 
	 * @param bi
	 * @param g2d
	 * @param seal
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static BufferedImage drawFont2(BufferedImage bi, Graphics2D g2d, SquareSeal seal) {
		// 位置调整
		int fixH = 5;
		int fixW = 2;
		// 设置字体
		Font f = new Font(seal.getNameFont().getFamily(), Font.BOLD, seal.getNameFont().getSize());
		g2d.setFont(f);
		FontRenderContext context = g2d.getFontRenderContext();
		Rectangle2D rectangle = f.getStringBounds(seal.getNameFont().getText().substring(0, 1), context);
		int marginW = fixW + seal.getBorder();
		float marginH = (float) ((Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH);
		g2d.drawString(seal.getNameFont().getText().substring(0, 1), marginW, marginH);
		marginW += Math.abs(rectangle.getCenterX()) * 2 + (seal.getNameFont().getSpace() == null ? INIT_BEGIN : seal.getNameFont().getSpace());
		g2d.drawString(seal.getNameFont().getText().substring(1), marginW, marginH);

		// 拉伸
		BufferedImage nbi = new BufferedImage(seal.getWidth(), seal.getHeight(), bi.getType());
		Graphics2D ng2d = nbi.createGraphics();
		ng2d.setPaint(seal.getColor());
		ng2d.drawImage(bi, 0, 0, seal.getWidth(), seal.getHeight(), null);

		// 画正方形
		ng2d.setStroke(new BasicStroke(seal.getBorder()));
		ng2d.drawRect(0, 0, seal.getWidth(), seal.getHeight());
		ng2d.dispose();
		return nbi;
	}

	/**
	 * 画3字
	 * 
	 * @param bi
	 * @param g2d
	 * @param seal
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static BufferedImage drawFont3(BufferedImage bi, Graphics2D g2d, SquareSeal seal) {
		// 位置调整
		int fixH = 5;
		int fixW = 2;
		// 设置字体
		Font f = new Font(seal.getNameFont().getFamily(), Font.BOLD, seal.getNameFont().getSize());
		g2d.setFont(f);
		FontRenderContext context = g2d.getFontRenderContext();
		Rectangle2D rectangle = f.getStringBounds(seal.getNameFont().getText().substring(0, 1), context);
		int marginW = fixW + seal.getBorder();
		float marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH;
		int oldW = marginW;
		marginW += rectangle.getCenterX() * 2 + (seal.getNameFont().getSpace() == null ? INIT_BEGIN : seal.getNameFont().getSpace());
		g2d.drawString(seal.getNameFont().getText().substring(0, 1), marginW, marginH);

		// 拉伸
		BufferedImage nbi = new BufferedImage(seal.getWidth(), seal.getHeight(), bi.getType());
		Graphics2D ng2d = nbi.createGraphics();
		ng2d.setPaint(seal.getColor());
		ng2d.drawImage(bi, 0, 0, seal.getWidth(), seal.getHeight(), null);

		// 画正方形
		ng2d.setStroke(new BasicStroke(seal.getBorder()));
		ng2d.drawRect(0, 0, seal.getWidth(), seal.getHeight());
		ng2d.dispose();
		bi = nbi;

		// 画文字
		g2d = bi.createGraphics();
		g2d.setPaint(seal.getColor());
		g2d.setFont(f);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.drawString(seal.getNameFont().getText().substring(1, 2), oldW, marginH += fixH);
		rectangle = f.getStringBounds(seal.getNameFont().getText(), context);
		marginH += Math.abs(rectangle.getHeight());
		g2d.drawString(seal.getNameFont().getText().substring(2, 3), oldW, marginH);
		return bi;
	}

	/**
	 * 画4字
	 * 
	 * @param bi
	 * @param seal
	 * @return
	 * 
	 * @author 延晓磊
	 *
	 * @since 2020年6月9日
	 */
	private static BufferedImage drawFont4(BufferedImage bi, SquareSeal seal) {
		// 位置调整
		int fixH = 10;
		int fixW = 2;
		// 拉伸
		BufferedImage nbi = new BufferedImage(seal.getWidth(), seal.getHeight(), bi.getType());
		Graphics2D ng2d = nbi.createGraphics();
		ng2d.setPaint(seal.getColor());
		ng2d.drawImage(bi, 0, 0, seal.getWidth(), seal.getHeight(), null);

		// 画正方形
		ng2d.setStroke(new BasicStroke(seal.getBorder()));
		ng2d.drawRect(0, 0, seal.getWidth(), seal.getHeight());
		ng2d.dispose();
		bi = nbi;

		// 画文字
		Graphics2D g2d = bi.createGraphics();
		g2d.setPaint(seal.getColor());
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		FontRenderContext context = g2d.getFontRenderContext();
		Font f = new Font(seal.getNameFont().getFamily(), Font.BOLD, seal.getNameFont().getSize());
		g2d.setFont(f);
		Rectangle2D rectangle = f.getStringBounds(seal.getNameFont().getText().substring(0, 1), context);
		int marginW = fixW + seal.getBorder();
		float marginH = (float) (Math.abs(rectangle.getCenterY()) * 2 + marginW) + fixH;
		g2d.drawString(seal.getNameFont().getText().substring(2, 3), marginW, marginH);
		int oldW = marginW;
		marginW += Math.abs(rectangle.getCenterX()) * 2 + (seal.getNameFont().getSpace() == null ? INIT_BEGIN : seal.getNameFont().getSpace());
		g2d.drawString(seal.getNameFont().getText().substring(0, 1), marginW, marginH);
		marginH += Math.abs(rectangle.getHeight());
		g2d.drawString(seal.getNameFont().getText().substring(3, 4), oldW, marginH);
		g2d.drawString(seal.getNameFont().getText().substring(1, 2), marginW, marginH);
		return bi;
	}
}
